const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    state: '',
    distance: '附近',
    storename: '店铺分类',
    sorttype: '智能排序',
    distanceid: '',
    typeid: '',
    sortid: '',
    distanceArr: [
      {
        id: 0,
        name: '全部',
      },
      {
        id: 1,
        name: '0.5km',
      },
      {
        id: 2,
        name: '1km',
      },
      {
        id: 3,
        name: '2km',
      },
      {
        id: 4,
        name: '5km',
      },
      {
        id: 5,
        name: '10km',
      },
      {
        id: 6,
        name: '>10km',
      },
    ],
    storeArr: [],
    sortArr: [
      {
        id: 0,
        name: '全部',
      },
      {
        id: 1,
        name: '最近距离',
      },
      {
        id: 2,
        name: '最新入驻',
      },
    ],
    theme: app.themeData,
    shoplist: [],
    page: 1,
    more: true,
    abnorData: {
      imgSrc: '/common/static/images/abnor-07.png',
      text: '暂无相关店铺',
    },
  },

  onLoad(options) {
    this.loadStoreType();
    let id = options.id ? options.id : 0;
    this.loadStoreList(0, id, 0);
    this.setData({
      storename: options.name ? decodeURIComponent(options.name) : '全部',
      typeid: id,
      fromHome:
        (app.ws_micro_shop_globalData &&
          app.ws_micro_shop_globalData.fromHome) ||
        false,
    });
  },

  loadStoreType(tid) {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.default.storecate,
      success: res => {
        if (res.code === 0) {
          let { list } = res.data;
          for (let i in list) {
            if (Number(tid) === Number(list[i].id)) {
              that.setData({
                typeid: tid,
                storename: list[i].name,
              });
            }
          }
          list.unshift({
            id: 0,
            name: '全部',
            pic_url: '',
          });
          that.setData({
            storeArr: list,
          });
        }
      },
    });
  },

  onReachBottom() {
    let { distanceid, typeid, sortid } = this.data;
    this.loadStoreList(distanceid, typeid, sortid);
  },

  showModelCover(e) {
    let old_state = Number(this.data.state);
    let new_state = Number(e.currentTarget.dataset.state);
    if (old_state === new_state) {
      this.setData({
        state: '',
      });
    } else {
      this.setData({
        state: new_state,
      });
    }
  },

  selectDistance(e) {
    let { distanceArr: list, typeid, sortid } = this.data;
    let { idx } = e.currentTarget.dataset;
    this.setData({
      distance: list[idx].name,
      distanceid: list[idx].id,
      page: 1,
      shoplist: [],
      more: true,
    });
    this.loadStoreList(list[idx].id, typeid, sortid);
  },

  selectStoreType(e) {
    let { distanceid, storeArr: list, sortid } = this.data;
    let { idx } = e.currentTarget.dataset;
    this.setData({
      storename: list[idx].name,
      typeid: list[idx].id,
      page: 1,
      shoplist: [],
      more: true,
    });
    this.loadStoreList(distanceid, list[idx].id, sortid);
  },

  selectSort(e) {
    let { distanceid, typeid, sortArr: list } = this.data;
    let { idx } = e.currentTarget.dataset;
    this.setData({
      sorttype: list[idx].name,
      sortid: list[idx].id,
      page: 1,
      shoplist: [],
      more: true,
    });
    this.loadStoreList(distanceid, typeid, list[idx].id);
  },

  loadStoreList(did, tid, sid) {
    let that = this;
    let { shoplist, page, more } = that.data;
    wx.getLocation({
      success: function(e) {
        if (e.errMsg !== 'getLocation:ok') {
          return;
        }
        if (more) {
          wx.showLoading({
            title: '加载中...',
          });
          app.ws_micro_shoprequest({
            url: api.default.storelist,
            data: {
              page,
              longitude: e.longitude,
              latitude: e.latitude,
              distance: did,
              category: tid,
              sort: sid,
            },
            success: res => {
              wx.hideLoading();
              if (res.code === 0) {
                let list = res.data.list;
                wx.setNavigationBarTitle({
                  title: res.data.store_list_alias || '门店列表',
                });
                if (list && list.length > 0) {
                  page += 1;
                  that.setData({
                    shoplist: shoplist.concat(list),
                    page,
                  });
                }
              }
            },
          });
        }
      },
    });
  },

  toShopHome(e) {
    let shopid = e.currentTarget.dataset.sid;
    app.ws_micro_shop_globalData.shopid = shopid;
    wx.setStorageSync('latestShopId', shopid);
    app.ws_micro_shop_globalData.fromHome = false;
    app.util.navigateTo('/ws_micro_shop/pages/index/index', {}, true);
  },

  homeNavigation(e) {
    app.ws_micro_shop_globalData.fromHome = true;
    let url =
      e.currentTarget.dataset.type === 'home'
        ? 'ws_micro_shop/pages/home/index'
        : 'ws_micro_shop/pages/user/user';
    app.util.navigateTo(url, {}, true);
  },
});
